

// 对象数组排序方法
export function objArraySort(objArr, key, des = "asc", length = 12) {
  let result = objArr.slice(0);
  result = result.filter((item) => item.name.length >= length);
  // 过滤deviceId重复的数据
  result = result.filter((item, index) => {
    return (
      index ===
      result.findIndex((obj) => obj.deviceId == item.deviceId && obj.name != "")
    );
  });
  // 升序
  if (des == "asc") {
    return result.sort((a, b) => a[key] - b[key]);
  } else {
    return result.sort((a, b) => a[key] - b[key]).reverse();
  }
}

/**
 *
 * @param { String } code  // 参数code
 * @param { String } str  // 参数
 * @returns
 */
// 写入参数格式转换
export function setValstr(code, str) {
  return code.slice(0, 2) + strToHex(str) + code.slice(code.length - 2);
}

// 字符串转16进制
export function strToHex(str) {
  var val = "";
  for (var i = 0; i < str.length; i++) {
    if (val == "") val = str.charCodeAt(i).toString(16);
    else val += str.charCodeAt(i).toString(16);
  }
  return val;
}

// buff 转 str
export function arrayBufferToString(buffer) {
  let x = new Uint8Array(buffer);
  // log(x)
  let strHex = "";
  let str = "";
  for (let i = 0; i < x.length; i++) {
    strHex = strHex + x[i].toString(16).padStart(2, "0");
    str = str + String.fromCharCode(x[i]);
  }
  return strHex;
}


//将16进制转为字符串
export function hexToString(str) {
  var val = "",
    len = str.length / 2;
  for (var i = 0; i < len; i++) {
    val += String.fromCharCode(parseInt(str.substr(i * 2, 2), 16));
  }
  return utf8to16(val);
}

//处理中文乱码问题
export function utf8to16(str) {
  var out, i, len, c;
  var char2, char3;
  out = "";
  len = str.length;
  i = 0;
  while (i < len) {
    c = str.charCodeAt(i++);
    switch (c >> 4) {
      case 0:
      case 1:
      case 2:
      case 3:
      case 4:
      case 5:
      case 6:
      case 7:
        out += str.charAt(i - 1);
        break;
      case 12:
      case 13:
        char2 = str.charCodeAt(i++);
        out += String.fromCharCode(((c & 0x1f) << 6) | (char2 & 0x3f));
        break;
      case 14:
        char2 = str.charCodeAt(i++);
        char3 = str.charCodeAt(i++);
        out += String.fromCharCode(
          ((c & 0x0f) << 12) | ((char2 & 0x3f) << 6) | ((char3 & 0x3f) << 0)
        );
        break;
    }
  }
  // console.log("16进制转字符串", out);
  return out;
}

// 格式化日期 前台传值方式  引用类.dateFormat(1402233166999,"yyyy-MM-dd hh:mm:ss")
export function dateFormat(date, fmt = "yyyy-MM-dd hh:mm:ss") {
  let getDate = new Date(date);
  let o = {
    "M+": getDate.getMonth() + 1,
    "d+": getDate.getDate(),
    "h+": getDate.getHours(),
    "m+": getDate.getMinutes(),
    "s+": getDate.getSeconds(),
    "q+": Math.floor((getDate.getMonth() + 3) / 3),
    S: getDate.getMilliseconds(),
  };
  if (/(y+)/.test(fmt)) {
    fmt = fmt.replace(
      RegExp.$1,
      (getDate.getFullYear() + "").substr(4 - RegExp.$1.length)
    );
  }
  for (let k in o) {
    if (new RegExp("(" + k + ")").test(fmt)) {
      fmt = fmt.replace(
        RegExp.$1,
        RegExp.$1.length === 1 ? o[k] : ("00" + o[k]).substr(("" + o[k]).length)
      );
    }
  }
  return fmt;
}

// 将一串16进制转成对应的10进制数组
export function hex2intArr(str) {
  if (str.length % 2 !== 0) {
    console.error("输入的16进制数格式不正确，字符个数不是双数。");
    return;
  }
  const arr = [];
  for (let i = 0; i < str.length; i += 2) {
    arr.push(hex2int(str.substr(i, 2)));
  }
  return arr;
}

// 将一个16进制转10进制数字
export function hex2int(hex) {
  var len = hex.length,
    a = new Array(len),
    code;
  for (var i = 0; i < len; i++) {
    code = hex.charCodeAt(i);
    if (48 <= code && code < 58) {
      code -= 48;
    } else {
      code = (code & 0xdf) - 65 + 10;
    }
    a[i] = code;
  }

  return a.reduce(function (acc, c) {
    acc = 16 * acc + c;
    return acc;
  }, 0);
}

// 16进制转汉字
export function hexToCn(str) {
  var val = "";
  var arr = str.split(",");
  for (var i = 0; i < arr.length; i++) {
    val += String.fromCharCode(parseInt(arr[i], 16));
  }
  return val;
}
